#include "umlhighlighter.h"

UmlHighlighter::UmlHighlighter(QTextDocument *document) :
    QSyntaxHighlighter(document)
{
}

void UmlHighlighter::highlightBlock(const QString &text)
{

    qDebug() << "highlight block";
    QHashIterator<Type, QRegExp> iter(this->_regexForType);
    while (iter.hasNext()) {
        iter.next();
        Type type = iter.key();
        const QRegExp &regex = iter.value();
        //int index = regex.indexIn(text);
        /*while (index > -1) {
            int length = regex.matchedLength();

            this->applyFormat(type, index, length);

            index = regex.indexIn(text, index + length);
        }*/

        int pos = 0;
        while ((pos = regex.indexIn(text, pos)) != -1) {
            int length = regex.matchedLength();
            this->applyFormat(type, pos, length);
            pos += length;
        }
    }

}

void UmlHighlighter::addRegex(Type type, const QString &pattern, bool minimal)
{
    QRegExp regex(pattern);
    regex.setPatternSyntax(QRegExp::RegExp2);
    regex.setMinimal(minimal);

    this->_regexForType.insert(type, regex);
}
